﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WHControlLib.IconFont;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WHControlLib
{

    [ToolboxItem(false)]
    public partial class baseTextBoxEx : System.Windows.Forms.TextBox
    {
        Rectangle TextRect,IconRect;
        

        /// <summary>
        /// 自绘消息
        /// </summary>
        const int WM_paint=0xf;
       /// <summary>
       /// 控件颜色改变或编辑时消息
       /// </summary>
        const int WM_CtlColOrEdit = 0x133;

        #region 属性定义

        private string  _waterMarkstring="";
        [Category("A我的"), Description("水印文字内容，默认 ，无，"), Browsable(true)]
        public string  WaterMarkstring
        {
            get { return _waterMarkstring; }
            set { _waterMarkstring= value; }
        }
        private bool _isDisplayWaterMark=false;
        [Category("A我的"), Description("是否开启水印文字，默认 ，否，"), Browsable(true)]
        public bool IsDisplayWaterMark
        {
            get { return _isDisplayWaterMark; }
            set { _isDisplayWaterMark = value; Invalidate(); }
        }

        private Color _watermarkColor=Color.Gray    ;
       
        [Category("A我的"), Description("水印文字的颜色，默认 ，灰色，"), Browsable(true)]
        public Color WatermarkColor
        {
            get { return _watermarkColor; }
            set { _watermarkColor = value;Invalidate(); }
        }



        private int _waterMarkDire ;
        [Category("A我的"), Description("水印文字对齐方式，默认 ，左对齐，"), Browsable(true)]
        public int WaterMarkDire
        {
            get { return _waterMarkDire; }
            set { _waterMarkDire = value; }
        }
        //图标字体的属性设置

        private bool _isUseIconFont;
        [Category("A我的字体图标"), Description("是否使用图标字体，与是否 IsShowMyImage 互斥，用图片不能用图标，默认 false"), Browsable(true)]
        public bool IsUseIconFont
        {
            get { return _isUseIconFont; }
            set
            {
          

                _isUseIconFont = value;
                Invalidate();
            }
        }

        private Color _iconFontColor = Color.Silver;
        [Category("A我的字体图标"), Description("图标字体的颜色，，默认 黑色"), Browsable(true)]
        public Color IconFontColor
        {
            get { return _iconFontColor; }
            set { _iconFontColor = value; Invalidate(); }
        }



        private int _iconFont = 61452;
        //[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        //[Editor(typeof(FontImagePropertyEditor), typeof(System.Drawing.Design.UITypeEditor))]
        //[Category("A我的字体图标"), Description("图标字体的十进制编号"), Browsable(true)]
        public int IconFont
        {
            get { return _iconFont; }
            set
            {
                _iconFont = value;
                Invalidate();

            }

        }
        private int _iconFontSize = 15;
        [Category("A我的字体图标"), Description("图标字体大小，默认15，在5-105之间"), Browsable(true)]
        public int IconFontSize
        {
            get { return _iconFontSize; }
            set { _iconFontSize = value; Invalidate(); }
        }

       //public enum IconFontDes
       // {
       //     Left,Right
       // }

        private  int _myIconFontDes;
        [Category("A我的字体图标"), Description("图标字体的显示方向，默认 左边"), Browsable(true)]
        public int MyIconFontDes
        {
            get { return _myIconFontDes; }
            set { _myIconFontDes = value; Invalidate(); }
        }
        private Size _iconFontOffset;
        [Category("A我的字体图标"), Description("图标字体的 位置调节"), Browsable(true)]
        public Size IconFontOffset
        {
            get { return _iconFontOffset; }
            set { _iconFontOffset = value; Invalidate(); }
        }





        #endregion


        //bool IsNeedCLearMark;
        public baseTextBoxEx()
        {
           
            InitializeComponent();
            this.AutoSize = false;

        }


        protected override void OnGotFocus(EventArgs e)
        {
            //base.OnGotFocus(e);


        }

        private void IsNeedWaterMark()
        {
            if ( string.IsNullOrEmpty(this.Text) /*&& !this.Focused*/ && IsDisplayWaterMark)
            {
                try
                {
                    
                    DrawMarkText();

                }
                catch (Exception)
                { }
            }
            else
            {
                ClearMarkText();
            }
        }

        protected override void WndProc(ref Message m)

        {
            base.WndProc(ref m);

            if (m.Msg == WM_paint || m.Msg == WM_CtlColOrEdit )
            {
                IsNeedWaterMark();

            }

        }


        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            IsNeedWaterMark();


        }


        void ClearMarkText()
        {
            
            if (Text.Length<1)
            {

          string s = this.Text;
            using (Graphics g = this.CreateGraphics())
            {
                g.Clear(this.BackColor);
            }
            Text = s; 
            }
            
        }
        void DrawMarkText( )
        {


            using (SolidBrush WaterFontBrush = new SolidBrush(WatermarkColor))
            {

                StringFormat sf = new StringFormat();
                switch (WaterMarkDire)
                {
                    case 0:
                        sf.Alignment = StringAlignment.Near; 
                        break;
                    case 1:
                        sf.Alignment =StringAlignment.Center;
                        break;
                    case 2:
                        sf.Alignment = StringAlignment.Far;
                        break;
                    default:
                        sf.Alignment = StringAlignment.Center;
                        break;
                }

               
              
                
                sf.LineAlignment = StringAlignment.Center;

                using (Graphics g=this.CreateGraphics())
                {
                    if (!IsUseIconFont)
                    {
                        TextRect = this.ClientRectangle;
                       g.DrawString(WaterMarkstring,this.Font,WaterFontBrush,TextRect,sf);
                        return;
                    }
                    else
                   //当启用图标绘制时候
                    {
                        Rectangle rect = this.ClientRectangle;
                        Bitmap IconBitmap= FontImageHelper.GetImageFontBitmap(IconFont, IconFontColor, IconFontSize);
                       
                        
                        IconRect.Y =(int)( rect.Height/2-IconBitmap.Height/2)+2+IconFontOffset.Height;
                        IconRect.Width = IconBitmap.Width + 5;
                        IconRect.Height = IconBitmap.Height + 5;

                        
                         
                        switch (MyIconFontDes)
                        {
                            case 0:
                                if (WaterMarkDire == 0)
                                {

                                    IconRect.X = 2+IconFontOffset.Width;
                                    TextRect.Width = rect.Width - IconBitmap.Width;
                                    TextRect.Height = rect.Height;
                                    TextRect.X = IconRect.Width;
                                    TextRect.Y = 0 ;
                                }
                                else TextRect = rect;
                              
                                g.DrawImage(IconBitmap,IconRect);
                                g.DrawString(WaterMarkstring, this.Font, WaterFontBrush, TextRect, sf);

                                break;
                            case 1:
                                if (WaterMarkDire==2)
                                {
                                IconRect.X = rect.Width - IconBitmap.Width - 5 + IconFontOffset.Width;
                                TextRect.Width = rect.Width - IconBitmap.Width;
                                TextRect.Height = rect.Height;
                                TextRect.X = 0;
                                TextRect.Y = 0;
                                }
                                else TextRect = rect;

                                g.DrawImage(IconBitmap, IconRect);
                                g.DrawString(WaterMarkstring, this.Font, WaterFontBrush, TextRect, sf);
                                break;
                            default:
                                break;
                        }


                   

                    }
                    
                }

            }

        }



          

     
        //   win32 发窗口消息方法 画水印

        //if (IsDisplayWaterMark&& WaterMarkString!=""&& !textbox.Focused)
        //{
        //   SendMessage(this.textbox.Handle, EM_SETCUEBANNER, 0, WaterMarkString);
        //}


    }
}
